# Maintainer: David Runge <dvzrv@archlinux.org>

pkgbase=dragonfly-reverb
pkgname=(
  dragonfly-reverb
  dragonfly-reverb-{clap,lv2,standalone,vst,vst3}
)
pkgver=3.2.10
pkgrel=2
pkgdesc="A concert hall reverb and a room reverb"
arch=(loong64 x86_64)
url="https://michaelwillis.github.io/dragonfly-reverb/"
license=(GPL3)
makedepends=(
  freeverb3
  gcc-libs
  glibc
  gendesk
  jack
  libglvnd
  libsamplerate
  libx11
  libxext
  lv2
)
checkdepends=(lv2lint)
source=(https://github.com/michaelwillis/$pkgbase/releases/download/$pkgver/$pkgbase-$pkgver-src.tar.xz)
sha512sums=('772b70169970e5ec68270d3cfa690ad1b258a2df79430ecb36cae78654a761cb9b495a769daef37307af9960106c302df4d61094f95eb4f8619a0b764aa8d019')
b2sums=('247a29af2070b191aa17c457ee333b090e17bec8abd82812cf980aad8b534006b8e3c3bfede8195d0729ee1319e7b145d79abd41b015f794e3d02e679b5a4bd6')
_names=(
  DragonflyEarlyReflections
  DragonflyHallReverb
  DragonflyPlateReverb
  DragonflyRoomReverb
)
_common_depends=(
  gcc-libs
  glibc
  libglvnd
  freeverb3
  libx11
  libxext
)

prepare() {
  declare -A _description=(
    ["DragonflyEarlyReflections"]="A free stereo-to-stereo early reflections reverb effect"
    ["DragonflyHallReverb"]="A free stereo-to-stereo concert hall reverb effect"
    ["DragonflyPlateReverb"]="A free stereo-to-stereo plate reverb effect"
    ["DragonflyRoomReverb"]="A free stereo-to-stereo room reverb effect"
  )
  declare -A _generic=(
    ["DragonflyEarlyReflections"]="Early Reflections Reverb"
    ["DragonflyHallReverb"]="Concert Hall Reverb"
    ["DragonflyPlateReverb"]="Plate Reverb"
    ["DragonflyRoomReverb"]="Room Reverb"
  )

  cd $pkgbase-$pkgver

  # explicitly remove vendored freeverb as it causes issues: https://github.com/michaelwillis/dragonfly-reverb/issues/129
  rm -frv common/freeverb/

  for name in "${_names[@]}"; do
    gendesk -n \
            --pkgname io.github.michaelwillis.$name \
            --name $name \
            --exec $name \
            --pkgdesc "${comments[$name]}" \
            --genericname "${generic[$name]}" \
            --categories "AudioVideo;Audio"
  done
}

build() {
  export SYSTEM_FREEVERB3="true"
  make -C $pkgbase-$pkgver
}

check() {
  local _plugin_name=""
  declare -A _urls=(
    ["DragonflyHallReverb"]='https://github.com/michaelwillis/dragonfly-reverb'
    ["DragonflyEarlyReflections"]='urn:dragonfly:early'
    ["DragonflyPlateReverb"]='urn:dragonfly:plate'
    ["DragonflyRoomReverb"]='urn:dragonfly:room'
  )

  for _plugin_name in "${_names[@]}"; do
    lv2lint -Mpack -s "lv2_generate_ttl" -I $pkgbase-$pkgver/bin/$_plugin_name.lv2/ "${_urls[$_plugin_name]}"
  done
}

package_dragonfly-reverb() {
  depends=(dragonfly-reverb-{clap,lv2,standalone,vst,vst3}=$pkgver-$pkgrel)
}

package_dragonfly-reverb-clap() {
  pkgdesc+=" - CLAP plugin"
  depends=(
    "${_common_depends[@]}"
    clap-host
  )
  groups=(
    clap-plugins
    pro-audio
  )

  cd $pkgbase-$pkgver
  for name in "${_names[@]}"; do
    install -vDm 755 bin/$name.clap -t "$pkgdir/usr/lib/clap/"
  done
}

package_dragonfly-reverb-lv2() {
  pkgdesc+=" - LV2 plugin"
  groups=(
    lv2-plugins
    pro-audio
  )
  depends=(
    "${_common_depends[@]}"
    lv2-host
  )

  cd $pkgbase-$pkgver
  for name in "${_names[@]}"; do
    install -vDm 755 bin/$name.lv2/*.so -t "$pkgdir/usr/lib/lv2/$name.lv2/"
    install -vDm 644 bin/$name.lv2/*.ttl -t "$pkgdir/usr/lib/lv2/$name.lv2/"
  done
}

package_dragonfly-reverb-standalone() {
  pkgdesc+=" - standalone"
  groups=(
    pro-audio
  )
  conflicts=(dragonfly-reverb-jack)
  replaces=(dragonfly-reverb-jack)
  depends=(
    "${_common_depends[@]}"
  )

  cd $pkgbase-$pkgver
  for name in "${_names[@]}"; do
    install -vDm 755 bin/$name -t "$pkgdir/usr/bin"
  done
  install -vDm 644 ./*.desktop -t "$pkgdir/usr/share/applications/"
  install -vDm 644 README.md -t "$pkgdir/usr/share/doc/$pkgname"
}

package_dragonfly-reverb-vst() {
  pkgdesc+=" - VST plugin"
  groups=(
    pro-audio
    vst-plugins
  )
  depends=(
    "${_common_depends[@]}"
    vst-host
  )

  cd $pkgbase-$pkgver
  for name in "${_names[@]}"; do
    install -vDm 755 bin/$name-vst.so -t "$pkgdir/usr/lib/vst"
  done
}

package_dragonfly-reverb-vst3() {
  pkgdesc+=" - VST3 plugin"
  groups=(
    pro-audio
    vst3-plugins
  )
  depends=(
    "${_common_depends[@]}"
    vst3-host
  )

  cd $pkgbase-$pkgver
  for name in "${_names[@]}"; do
    install -vDm 755 bin/$name.vst3/Contents/`uname -m`-linux/$name.so -t "$pkgdir/usr/lib/vst3/"
  done
}
